/**
 * LoginDialog.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.view.login;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.openqa.selenium.Alert;

import webcrawler.activities.Main;
import webcrawler.activities.data.DataFile;
import webcrawler.activities.data.DataManager;

/**
 * The Class LoginDialog.
 * 
 * @author Gerson Groth
 */
@SuppressWarnings("serial")
public class LoginDialog extends JDialog {

	/** The content panel. */
	private JPanel contentPanel;

	/** The jtf username. */
	private JTextField jtfUsername;

	/** The password field. */
	private JPasswordField passwordField;

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		setTitle("Login");
		createPanel();

		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				if (e.getID() == WindowEvent.WINDOW_CLOSING) {
					dispose();
				}
			}
		});

		setBounds(100, 100, 311, 274);
		getContentPane().setLayout(null);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);

		JPanel garminPanel = new JPanel();
		garminPanel.setForeground(Color.BLACK);
		garminPanel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Put your garmin account",
				TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLACK));
		garminPanel.setBounds(10, 11, 275, 181);
		garminPanel.setOpaque(false);
		contentPanel.add(garminPanel);
		garminPanel.setLayout(null);

		JPanel loginPanel = new JPanel();
		loginPanel.setBounds(10, 22, 255, 138);
		loginPanel.setBackground(new Color(255, 255, 255, 0));
		loginPanel.setOpaque(false);
		garminPanel.add(loginPanel);
		loginPanel.setLayout(null);

		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(57, 11, 62, 14);
		loginPanel.add(lblUsername);

		jtfUsername = new JTextField();
		jtfUsername.setBounds(57, 36, 140, 20);
		loginPanel.add(jtfUsername);
		jtfUsername.setColumns(10);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(57, 67, 62, 14);
		loginPanel.add(lblPassword);

		passwordField = new JPasswordField();
		passwordField.setBounds(57, 92, 140, 20);
		loginPanel.add(passwordField);

		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		btnSave.setBounds(62, 203, 80, 23);
		contentPanel.add(btnSave);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnCancel.setBounds(158, 203, 80, 23);
		contentPanel.add(btnCancel);

		this.jtfUsername.setText(DataFile.getInstance().getUsername());
		this.passwordField.setText(DataManager.decryptPass(DataFile
				.getInstance().getPassword()));

	}

	/**
	 * Save.
	 */
	private void save() {

		char[] ch = passwordField.getPassword();
		String pass = new String(ch);
		// if ("".equals(jtfUsername.getText()) || "".equals(pass)) {
		// JOptionPane.showMessageDialog(this,
		// "Please type your garmin account", "Attention!",
		// JOptionPane.WARNING_MESSAGE);
		// } else {
		DataFile.getInstance().setUsername(jtfUsername.getText());
		DataFile.getInstance().setPassword(DataManager.cryptPass(pass));

		DataManager.getINSTANCE().saveData();

		this.dispose();
		// }

	}

	/**
	 * Creates the panel.
	 */
	private void createPanel() {
		final Image imagemFundo;
		String fundo = "data/images/digital.jpg";

		try {
			// Inicializa a imagem do fundo
			ImageIcon imagem = new ImageIcon(fundo);
			imagemFundo = imagem.getImage();

			contentPanel = new JPanel() {
				{
					setOpaque(false);
				} // instance initializer

				public void paintComponent(Graphics g) {
					Dimension windowSize = getSize();
					g.drawImage(null, 0, 0, windowSize.width,
							windowSize.height, this);
					// g.drawImage(imagemFundo, 0, 0, windowSize.width,
					// windowSize.height, this);
					super.paintComponent(g);
				}
			};
			contentPanel.setBounds(0, 0, 295, 236);
		} catch (Exception e) {
			contentPanel = new JPanel();
		}

	}
}
